package com.cskaoyan.wordmemorize.controller.app;

import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.app.PayOrderDTO;
import com.cskaoyan.wordmemorize.dto.app.UserDTO;
import com.cskaoyan.wordmemorize.request.PaySuccessNotifyCommand;
import com.cskaoyan.wordmemorize.request.VocPaySaleCommand;
import com.cskaoyan.wordmemorize.service.UserPayService;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.*;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserRemindCommand;
import com.cskaoyan.wordmemorize.service.UserPayService;
import com.cskaoyan.wordmemorize.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    UserService userService;
    @Resource
    UserPayService userPayService;


    // 获取用户信息
    @GetMapping("info")
    public Result<UserDTO> getUserInfo() {
        Long userId = StpKit.USER.getLoginIdAsLong();
        UserDTO userInfo = userService.getUserInfo(userId);
        return Result.ok(userInfo);
    }

    @PostMapping("pay")
    public Result<PayOrderDTO> userPay(@RequestBody VocPaySaleCommand vocPaySaleCommand) {
        Long userId = StpKit.USER.getLoginIdAsLong();
        if (vocPaySaleCommand == null) {
            return Result.fail();
        }
        PayOrderDTO pay = userPayService.pay(vocPaySaleCommand, userId);
        return Result.ok(pay);

    }

    @PostMapping("pay/notice")
    public Result<String> getPayNotice(@RequestBody PaySuccessNotifyCommand paySuccessNotifyCommand) {
        System.out.println("paySuccessNotifyCommand = " + paySuccessNotifyCommand);
        String string = userPayService.paySuccess(paySuccessNotifyCommand);
        System.out.println("string = " + string);
        return Result.ok(string);
    }

    @GetMapping("/pay/{paySaleId}")
    public Result<Integer> getPatStatus(@PathVariable("paySaleId") Long paySaleId) {
        if (paySaleId < 0) {
            return Result.fail();
        }
        Integer i = userPayService.queryTradeStatus(paySaleId);
        return Result.ok(i);
    }

    @GetMapping("/pay/list")
    public Result<PageDTO<VocPaySaleDTO>> getPayHistory(@ModelAttribute PageRequest pageRequest) {
        if (pageRequest == null) {
            return Result.fail();
        }
        long loginIdAsLong = StpKit.USER.getLoginIdAsLong();
        PageDTO<VocPaySaleDTO> paySaleHistory = userPayService.getPaySaleHistory(loginIdAsLong, pageRequest);
        return Result.ok(paySaleHistory);
    }

    //更新提醒时间
    @PutMapping("remind")
    public Result updateUserRemind(@RequestBody UserRemindCommand command) {
        userService.updateUserRemind(command);
        return Result.ok();
    }

    //更新用户复习单词数量
    @PutMapping("day/voc")
    public Result updateVocCountOfDay(Integer count) {
        Long userId = StpKit.USER.getLoginIdAsLong();
        userService.updateVocCountOfDay(userId, count);
        return Result.ok();
    }

    //返回用户页面展示条目
    @GetMapping("voc/my/page")
    public Result<UserPageDTO> getUserPage() {
        Long userId = StpKit.USER.getLoginIdAsLong();
        UserPageDTO userPage = userService.getUserPage(userId);
        return Result.ok(userPage);
    }

    //返回用户单词上限统计
    @GetMapping("voc/bound/statistics")
    public Result<UserVocBoundStatisticsDTO> getUserVocBoundStatistics() {
        Long userId = StpKit.USER.getLoginIdAsLong();
        UserVocBoundStatisticsDTO statistics = userService.getUserVocBoundStatistics(userId);
        return Result.ok(statistics);
    }

    //返回用户单词上限变化
    @GetMapping("voc/bound/detail")
    public Result<PageDTO<DisplayUserVocBoundLogDTO>> getUserVocBoundLog(PageRequest pageRequest) {
        Long userId = StpKit.USER.getLoginIdAsLong();
        PageDTO<DisplayUserVocBoundLogDTO> log = userService.getUserVocBoundLog(pageRequest, userId);
        return Result.ok(log);
    }


    @GetMapping("level/info")
    public Result<DisplayUserLevelDTO> getLevelInfo() {
        Long userId = StpKit.USER.getLoginIdAsLong();
        System.out.println("userId = " + userId);
        DisplayUserLevelDTO userLevelInfo = userService.getUserLevelInfo(userId);

        return Result.ok(userLevelInfo);
    }

    @GetMapping("upgrade/info")
    public Result<NextLevelDTO> getUserNextDTO() {
        Long userId = StpKit.USER.getLoginIdAsLong();
        NextLevelDTO userNextDTO = userService.getUserNextDTO(userId);
        return Result.ok(userNextDTO);
    }
}
